package it.rap.applications.persistence.entities.filters;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.MappedSuperclass;
import javax.persistence.Transient;

@MappedSuperclass
public class TreeObject {
	@Transient
	private List<TreeObject> children = null;
	
	@Transient
	private TreeObject parent = null;

	@Transient
	private Boolean modified = false;
		
	@Column(nullable = false)
	private String name;
	
	public TreeObject(String name) {
		setName(name);		
	}

	public TreeObject() {
		super();
	}

	public void setParent(TreeObject parent) {
		this.parent = parent;
	}

	public TreeObject getParent() {
		return parent;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getName() {
		return name;
	}

	public void addChild(TreeObject child) {
		if ( children == null ) children = new ArrayList<TreeObject>();
		children.add(child);
		child.setParent(this);
	}

	public void removeChild(TreeObject child) {
		if ( children != null ){
			children.remove(child);
		}
		child.setParent(null);
	}

	public void removeAllChild() {
		children = null;
	}
	
	public TreeObject[] getChildren() {
		TreeObject[] result = null;
		if ( children != null && children.size() > 0 ){
			result = new TreeObject[children.size()];
			children.toArray(result);
		}
		return result;
	}

	public boolean hasChildren() {
		if ( children == null ) return false;
		return children.size() > 0;
	}

	@Override
	public String toString() {
		return name;
	}

	public Boolean isModified() {
		return modified;
	}

	public void setModified(Boolean modified) {
		this.modified = modified;
	}

}

